package com.leyou.page.service.Impl;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.PageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.leyou.common.constants.ReadisConstants.*;

@Service
public class PageServiceImpl implements PageService {


    @Autowired
    private ItemClient itemClient;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public String loadSpuVo(Long spuId) {
        //查询spu信息
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        //判断信息是否存在
        if (spuDTO == null || spuDTO.getId() == null){
            throw new LyException(404 ,"商品不存在");
        }
        //类型转换
        SpuVo spuVo = spuDTO.toEntity(SpuVo.class);
        spuVo.setCategoryIds(spuDTO.getCategoryIds());
        //序列化
        String json = JsonUtils.toJson(spuVo);
        //将获取的数据放到redis
        redisTemplate.opsForValue().set(SPU_KEY_PREFIX + spuId , json);
        //返回
        return json;
    }

    @Override
    public String loadSpuDetail(Long spuId) {
        // 查询spuDetail信息
        SpuDetailDTO detailDTO = itemClient.querySpuDetailById(spuId);
        if (detailDTO == null) {
            throw new LyException(404, "商品不存在！");
        }
        // 转为json格式
        String json = JsonUtils.toJson(detailDTO);
        //写入redis中
        redisTemplate.opsForValue().set(DETAIL_KEY_PREFIX + spuId, json);
        // 返回
        return json;
    }

    @Override
    public String loadSkuList(Long spuId) {
        //查询sku信息
        List<SkuDTO> skuDTOList = itemClient.querySkuBySpuId(spuId);
        if (skuDTOList == null || skuDTOList.size() <= 0){
            throw new LyException(404 ,"商品详情不存在");
        }
        //转为json格式
        String json = JsonUtils.toJson(skuDTOList);
        //写入redis中
        redisTemplate.opsForValue().set(SKU_KEY_PREFIX + spuId ,json);
        //返回
        return json;
    }

    @Override
    public String loadCategories(List<Long> ids) {
        //查询category信息
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        if (categoryDTOList ==null || categoryDTOList.size() <= 0){
            throw new LyException(404 ,"商品详情不存在");
        }
        //转换为vo
        List<CategoryVO> categoryVOList = categoryDTOList.stream()
                .map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class))
                .collect(Collectors.toList());
        //序列化json
        String json = JsonUtils.toJson(categoryVOList);
        //将数据放到redis
        redisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX + ids.get(2) ,json);
        //返回
        return json;
    }

    @Override
    public String loadBrand(Long brandId) {
        //查询品牌信息
        BrandDTO brandDTO = itemClient.queryBrandById(brandId);
        //转换为vo信息
        BrandVo brandVo = brandDTO.toEntity(BrandVo.class);
        //序列化json
        String json = JsonUtils.toJson(brandVo);
        //将数据放到redis
        redisTemplate.opsForValue().set(BRAND_KEY_PREFIX + brandId, json);
        //返回
        return null;
    }

    @Override
    public String loadSpec(Long categoryId) {
        //根据分类id查询规格组和规格组信息
        List<SpecGroupDTO> specGroupDTOS = itemClient.querySpecList(categoryId);
        if (specGroupDTOS == null || specGroupDTOS.size() <= 0){
            throw new LyException(404 ,"规格组信息不存在");
        }
        //类型转换vo
        ArrayList<SpecGroupVO> groupVoList = new ArrayList<>(specGroupDTOS.size());
        for (SpecGroupDTO specGroupDTO :specGroupDTOS){
            //创建一个groupVo
            SpecGroupVO specGroupVO = new SpecGroupVO();
            //放到一个集合中
            groupVoList.add(specGroupVO);
            //填写属性name
            specGroupVO.setName(specGroupDTO.getName());
            //填写属性params
            /*for (SpecParamDTO specParamDTO : specGroupDTO.getParams()){
                SpecParamVO specParamVO = new SpecParamVO();
                specParamVO.setName(specParamDTO.getName());
                specParamVO.setId(specParamDTO.getId());
                specParamVO.setGeneric(specParamDTO.getGeneric());
                specParamVO.setNumeric(specParamDTO.getNumeric());
                specParamVO.setUnit(specParamDTO.getUnit());
                specGroupVO.setParams(specParamVO);
            }*/
          List<SpecParamVO> paramVOList = specGroupDTO.getParams()
                    .stream().map(specParamDTO -> specParamDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            specGroupVO.setParams(paramVOList);
        }
        //序列化
        String json = JsonUtils.toJson(groupVoList);
        //写入到redis中
        redisTemplate.opsForValue().set(SPEC_KEY_PREFIX + categoryId ,json);
        //返回
        return json;
    }
    @Override
    public Boolean deleteSku(Long spuId){
        return redisTemplate.delete(SKU_KEY_PREFIX + spuId);
    }
}
